<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    div {
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        margin: 0 auto;
        text-align: center;
    }

    span {
        width: 150px;
        height: 20px;
        display: inline-block;
        border: 1px solid pink;
        vertical-align: top;
        margin-bottom: 15px;
    }
</style>

<body>
    <div>
        <h1>随机点名</h1>
        名字是:<span class="content"></span><br>
        <button class="start">开始</button>
        <button class="end">结束</button>
    </div>
    <script>
        let arr = ['张三', '李四', '王五', '赵六',]

        function getRandomInt(min, max) {
            min = Math.ceil(min);
            max = Math.floor(max);
            return Math.floor(Math.random() * (max - min)) + min; //不含最大值，含最小值
        }
        // 1.获取元素 两个按钮+div
        let start = document.querySelector('.start')
        let end = document.querySelector('.end')
        let content = document.querySelector('.content')
        // timer是全局变量
        let timer = 0
        // randomIndex是全局变量
        let randomIndex
        // 2.给按钮注册时间事件
        start.addEventListener('click', function () {
            // 随机抽取数据
            timer = setInterval(function () {
                let randomIndex = getRandomInt(0, arr.length - 1)
                content.innerHTML = arr[randomIndex]
            }, 66)
        })
        // 3.给结束按钮注册事件，本质是停止定时器
        end.addEventListener('click', function () {
            clearInterval(timer)
            arr.splice(randomIndex, 1)
            // console.log(arr);
            if (arr.length === 0) {
                content.innerHTML = '已经是最后一个了'
                // start.disabled = true
                // end.disabled = true
                start.disabled = end.disabled = true
            }
        })
    </script>
</body>

</html>